package com.peakmain.baselibrary.utils.file;

import android.os.Environment;
import android.util.Log;

import java.io.File;

/**
 * author ：Peakmain
 * createTime：2020/2/22
 * mail:2726449200@qq.com
 * describe：文件管理
 */
public class FileManager {
    private static volatile FileManager mInstance;
    //缓存根目录
    private static final String CACHE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "medidoctor";
    //图片缓存路径
    public static String imageFolderPath = CACHE_PATH + File.separator + "image" + File.separator;
    //语音保存地址
    private String audioFolderPath = CACHE_PATH + File.separator + "audio" + File.separator;
    //下载文件保存
    private String downloadFolderPath = CACHE_PATH + File.separator + "download" + File.separator;
    //日志缓存路径
    private String logFolderPath = CACHE_PATH + File.separator + "log" + File.separator;
    //视频缓存路径
    private String videoFolderPath = CACHE_PATH + File.separator + "video" + File.separator;
    //图片
    private File mImageFile;
    //音频
    private File mAudioFile;
    //下载
    private File mDownloadFile;
    //log日志
    private File mLogFile;
    //视频
    private File mVideoFile;

    public static FileManager getInstance() {
        if (mInstance == null) {
            synchronized (FileManager.class) {
                if (mInstance == null) {
                    mInstance = new FileManager();
                }
            }
        }
        return mInstance;
    }

    private FileManager() {
    }

    /**
     * 初始化
     */
    public void init() {
        //创建缓存一级文件夹
        if (existsdcard() && isFolderExists(CACHE_PATH)) {
            mImageFile = openDir(mImageFile, imageFolderPath);
            mAudioFile = openDir(mAudioFile, audioFolderPath);
            mDownloadFile = openDir(mDownloadFile, downloadFolderPath);
            mLogFile = openDir(mLogFile, logFolderPath);
            mVideoFile = openDir(mVideoFile, videoFolderPath);
        }
    }
    /**
     * 打开这个目录.
     *
     * @param dir  param
     * @param path param
     * @return null
     */
    private File openDir(File dir, String path) {
        if (isFolderExists(path)) {
            dir = new File(path);
        }
        return dir;
    }

    /**
     * 判断文件夹是否存在
     *
     * @param strFolder param
     * @return false
     */
    private boolean isFolderExists(String strFolder) {
        File file = new File(strFolder);
        if (!file.exists()) {
            boolean isDir = file.mkdir();
            if (isDir) {
                Log.d("TAG", "创建文件夹成功");
                return true;
            } else {
                Log.d("TAG", "创建文件夹失败");

                return false;
            }
        }
        return true;
    }

    /**
     * 判断SD卡是否存在
     *
     */
    public boolean existsdcard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 返回图片File文件
     *
     */
    public File getImageFile() {
        return mImageFile;
    }

    /**
     * 返回音频的文件
     *
     */
    public File getAudioFile() {
        return mAudioFile;
    }

    /**
     * 返回音频的文件
     */
    public File getDownloadFile() {
        return mDownloadFile;
    }

    /**
     * 返回日志的文件
     *
     */
    public File getLogFile() {
        return mLogFile;
    }

    /**
     * 返回缓存视频的File文件夹
     *
     */
    public File getVideoFile() {
        return mVideoFile;
    }

    /**
     * 返回音频的保存地址
     *
     */
    public String getAudioFolderPath() {
        return audioFolderPath;
    }

    /**
     * 返回一个文件下载地址
     *
     */
    public String getDownloadFolderPath() {
        return downloadFolderPath;
    }

    /**
     * 返回图片的保存地址
     *
     */
    public String getImageFolderPath() {
        return imageFolderPath;
    }

    /**
     * 视频的缓存路径
     *
     */
    public String getVideoFolderPath() {
        return videoFolderPath;
    }

    /**
     * 返回一个图片地址
     *
     * @return
     */
    public String getImagePath() {
        return getImageFolderPath() + FileUtil.getPhotoFileName();
    }

    /**
     * 返回一个视频地址
     *
     * @return
     */
    public String getVideoPath() {
        return getVideoFolderPath() + FileUtil.getVideoFileName();
    }

    /**
     * 获取缓存文件大小
     *
     * @return
     */
    public String getCacheSize() {
        return FileUtil.getAutoFileOrFilesSize(CACHE_PATH);
    }
}
